import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Query,
  ParseIntPipe,
} from '@nestjs/common';
import { WorksService } from './works.service';
import { CreateWorkDto } from './dto/create-work.dto';
import { UpdateWorkDto } from './dto/update-work.dto';
import { AuthGuard } from '../../common/guards/auth.guard';
import { CurrentUser } from '../../common/decorators/current-user.decorator';
import { Public } from '../../common/decorators/public.decorator';
import { WorkStatusEnum } from './entities/work.entity';
import type { JwtPayload } from '../auth/interfaces/jwt-payload.interface';

@Controller('works')
@UseGuards(AuthGuard)
export class WorksController {
  constructor(private readonly worksService: WorksService) { }

  /**
   * 创建作品
   */
  @Post('create')
  create(
    @Body() createWorkDto: CreateWorkDto,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.worksService.create(createWorkDto, user.sub);
  }

  /**
   * 获取作品列表（公开接口）
   * @param page 页码
   * @param limit 每页条数
   * @param category 作品分类
   * @param status 作品状态
   * @param keyword 搜索关键词（搜索标题、描述、标签）
   * @returns 作品列表
   */
  @Public()
  @Get('list')
  findAll(
    @Query('page') page?: string,
    @Query('limit') limit?: string,
    @Query('category') category?: string,
    @Query('status') status?: WorkStatusEnum,
    @Query('keyword') keyword?: string,
  ) {
    return this.worksService.findAll(
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 10,
      category,
      status,
      keyword,
    );
  }

  /**
   * 获取指定用户的作品列表
   */
  @Public()
  @Get('user/:userId')
  findByUser(
    @Param('userId', ParseIntPipe) userId: number,
    @CurrentUser() user: JwtPayload | undefined,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.worksService.findByUser(
      userId,
      user?.sub,
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 10,
    );
  }

  /**
   * 获取当前用户的作品列表
   */
  @Get('my-works')
  findMyWorks(
    @CurrentUser() user: JwtPayload,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
    @Query('status') status?: WorkStatusEnum,
  ) {
    return this.worksService.findByUser(
      user.sub,
      user.sub,
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 10,
      status,
    );
  }

  /**
   * 获取当前用户的收藏列表
   */
  @Get('my-collections')
  findMyCollections(
    @CurrentUser() user: JwtPayload,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.worksService.findMyCollections(
      user.sub,
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 10,
    );
  }

  /**
   * 获取当前用户的草稿箱
   */
  @Get('my-drafts')
  findMyDrafts(
    @CurrentUser() user: JwtPayload,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
  ) {
    return this.worksService.findByUser(
      user.sub,
      user.sub,
      page ? parseInt(page) : 1,
      limit ? parseInt(limit) : 10,
      WorkStatusEnum.DRAFT,
    );
  }

  /**
   * 获取作品详情
   */
  @Public()
  @Get(':id')
  findOne(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload | undefined,
  ) {
    return this.worksService.findOne(id, user?.sub);
  }

  /**
   * 更新作品
   */
  @Patch(':id')
  update(
    @Param('id', ParseIntPipe) id: number,
    @Body() updateWorkDto: UpdateWorkDto,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.worksService.update(id, updateWorkDto, user.sub);
  }

  /**
   * 删除作品
   */
  @Delete(':id')
  remove(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.worksService.remove(id, user.sub);
  }

  /**
   * 发布作品
   */
  @Post(':id/publish')
  publish(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.worksService.publish(id, user.sub);
  }

  /**
   * 保存为草稿
   */
  @Post(':id/draft')
  saveToDraft(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.worksService.saveToDraft(id, user.sub);
  }

  /**
   * 切换点赞状态（推荐使用，自动判断点赞/取消）
   */
  @Post(':id/toggle-like')
  toggleLike(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.worksService.toggleLike(id, user.sub);
  }

  /**
   * 点赞作品（标准 RESTful 接口）
   */
  @Post(':id/like')
  like(@Param('id', ParseIntPipe) id: number, @CurrentUser() user: JwtPayload) {
    return this.worksService.like(id, user.sub);
  }

  /**
   * 取消点赞（标准 RESTful 接口）
   */
  @Delete(':id/like')
  unlike(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.worksService.unlike(id, user.sub);
  }

  /**
   * 切换收藏状态（推荐使用，自动判断收藏/取消）
   */
  @Post(':id/toggle-collect')
  toggleCollect(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.worksService.toggleCollect(id, user.sub);
  }

  /**
   * 收藏作品（标准 RESTful 接口）
   */
  @Post(':id/collect')
  collect(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.worksService.collect(id, user.sub);
  }

  /**
   * 取消收藏（标准 RESTful 接口）
   */
  @Delete(':id/collect')
  uncollect(
    @Param('id', ParseIntPipe) id: number,
    @CurrentUser() user: JwtPayload,
  ) {
    return this.worksService.uncollect(id, user.sub);
  }
}
